﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class SubjectEdit : System.Web.UI.Page
    {
        int SubjectID;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                SubjectID = Convert.ToInt32(Request.QueryString["SubjectID"].ToString().Trim());
                //AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter dp = new AdminXSD.AwardTableAdapters.tbl_AwardTableAdapter();
                AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter dp = new AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter();
                DataTable dttbl = dp.Get_tbl_Subject_Select_All_By_SubjectID(SubjectID);
                this.txtSubjectName.Text = dttbl.Rows[0]["SubjectName"].ToString();
                this.txtContent.Value = Server.HtmlDecode(dttbl.Rows[0]["SubjectDescription"].ToString());

                foreach (ListItem item in this.cbbSubjectStatus.Items)
                {
                    if (item.Value == dttbl.Rows[0]["SubjectID"].ToString())
                    {
                        item.Selected = true;
                    }
                }

            }
            catch
            { 
                //bao that bai
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SubjectID = Convert.ToInt32(Request.QueryString["SubjectID"].ToString().Trim());
                AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter dp = new AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter();
                int ok = dp.tbl_Subject_Update(this.txtSubjectName.Text.Trim(), Server.HtmlDecode(this.txtContent.Value.Trim()), Convert.ToInt32(this.cbbSubjectStatus.SelectedValue.Trim()), SubjectID);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                SubjectID = Convert.ToInt32(Request.QueryString["SubjectID"].ToString().Trim());
                AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter dp = new AdminXSD.SubjectTableAdapters.tbl_SubjectTableAdapter();

                int ok = dp.tbl_Subject_Delete(SubjectID);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        /// <summary>
        /// Close dialog here
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Question.aspx");
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// Show dialog trong trường hợp update
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// Show dialog trong trường hợp delete
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

    }
}
